<?php

namespace app\api\controller;
use app\common\model\Search as SearchModel;
use app\common\model\Article as ArticleModel;
/**
 * 热搜
 *
 * @icon fa fa-circle-o
 */
class Search extends Base
{
    protected $ff,$valid,$noNeedLogin = ['*'];

    public function _initialize()
    {

        $this->ff = new SearchModel();//mysql操作

        $this->valid = new Validate();    //验证器
        parent::_initialize();
    }
    //点击搜索
    public function search_list()
    {
        //获取到个人搜索记录
        $uid = $this->auth->id;
        $where=[];
        if(!$uid){
            $where['ip'] =$this->request->ip();
        }else{
            $where['uid'] =$uid;
        }
        $where['status'] =1;
        $my_list = $this->ff
            ->field('max(createtime) end_createtime,keyword')
            ->where($where)
            ->group('keyword')
            ->limit(10)
            ->order('end_createtime desc')
            ->select();
        $hot_list = $this->ff
            ->field('count(keyword) numbers,keyword')
            ->limit(10)
            ->group('keyword')
            ->order('numbers desc')
            ->select();
        //获取到热搜词
        $this->success('获取成功',['my_list'=>$my_list,'hot_list'=>$hot_list],200);
    }

    //点击搜索 进行查询
    public function click_search()
    {
        $uid = $this->auth->id;
        $type = $this->request->post('type',1);//1:圈子2：医院
        $art_type = $this->request->post('art_type',1);//文章类型1:推荐2：专家
        $keyword = $this->request->post('keyword',"trim");
        $disease_id = $this->request->post('disease_id','');//病症id
        if(!$keyword){
            $this->error('请输入关键词');
        }
        $where['title|content'] = ['like',"%$keyword%"];//文章
        if($disease_id){
            $where2['disease_id'] = $disease_id;//文章
        }else{
            $where2['name'] = ['like',"%$keyword%"];//病症
        }
        $where3['name'] = ['like',"%$keyword%"];//医院
        $info = [];
        if($type == 1){
            //查询
            $info['bing'] = db('disease')->where($where2)->select();
            if($info['bing']){
                foreach ($info['bing'] as &$v){
                    $off_info = db('office')->where('id',$v['office_id'])->find();
                    $v['type_name'] = '';
                    if($off_info){
                        $v['type_name'] = $off_info['type_name'];
                    }
                }
            }
            $info['news'] = $this->getList($art_type,$where);
        }else{
            $hospital = new \app\common\model\Hospital();
            $info = $hospital->field('id,image,name,tag')->where($where3)->select();
            $info = json_decode(json_encode($info),true);
            if($info){
                foreach ($info  as &$v){
//                    if($v['image']){
//                        $v['image']=cdnurl($v['image'],true);
//                    }
                    if($v['tag']){
                        $v['tag']= db('tag')->where('id','in',$v['tag'])->select();
                    }
                }
            }
        }
        //插入数据
       if($disease_id){
           $data=[
               'keyword'=>$keyword,
               'type'=>$type,
               'status'=>1,
               'uid'=>$uid,
               'ip'=>$this->request->ip(),
           ];
           $this->ff->save($data);
       }
        $this->success('获取成功',$info,200);
    }

    //专家 获取文章 type=1
    public function getList($type=1,$where=[])
    {
        $uid = $this->auth->id?$this->auth->id:null;
        $articleModel =new  ArticleModel();
        //推荐1   专家分享3
        if($type ==1){
            $where['type']= 2;
        }
        else{
            //专家
            $where['type']= 1;
        }
        return  $articleModel->getList2($where,$uid);
    }


    public function clear_old()
    {
        $uid = $this->auth->id;
        $where=[];
        if(!$uid){
            $where['uid']=$uid;
        }else{
            $where['ip']=$this->request->ip();
        }
        $this->ff->where('uid',$uid)->update(['status'=>0]);
        $this->success('清除成功','',200);
    }
}